package com.imyours.webservice.services;

import com.imyours.model.user.UserEntity;
import com.imyours.model.util.HibernateUtil;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

/**
 * 修改密码接口
 * 
 * @author SLevenc
 * 
 */
@Service(name = "changePwd")
public class ChangePwd extends ContextService {

	public ChangePwd(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void execute() {
		UserEntity user = getUserEntity();
		if (user != null) {
			String oldPwd = context.getParameter().get("oldPwd");
			String newPwd = context.getParameter().get("newPwd");
			if (oldPwd != null && newPwd != null) {
				if (oldPwd.equals(user.getPassword())) {
					user.setPassword(newPwd);
					org.hibernate.Session se = HibernateUtil.getSessionFactory().openSession();
					se.beginTransaction();
					se.update(user);
					se.getTransaction().commit();
					se.close();
					context.success();
				}
			} else {
				context.fail(3, "invalidate Parameter");
			}
		}

	}

}
